package com.gaofei.buss.controller;

import com.gaofei.buss.feign.OrderFeign;
import com.gaofei.buss.feign.StorageFeign;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author : gaofee
 * @date : 10:48 2021/5/29
 * @码云地址 : https://gitee.com/itgaofee
 * 模拟买东西的业务
 */
@RestController
@RequestMapping("buss")
public class BussController {
    @Autowired
    OrderFeign orderFeign;

    @Autowired
    StorageFeign storageFeign;




    @RequestMapping("buy")
//    @Transactional  //本地事务来处理?本地事务不生效
    //使用分布式事务来回滚,见到异常就回滚
    @GlobalTransactional(rollbackFor = Exception.class)
    public Object buy() {
        //减库存和下单操作必须全部成功和全部失败.绝不允许一个成功,或者一个失败
        //减库存
        storageFeign.deduct("C1000", 20);

        if(true){
            throw new RuntimeException("出异常了!!!!!!!!!!!");
        }
        //下订单
        orderFeign.create("U1000","C1000",20);



        return "购买成功!!!";
    }
}
